<?php
//UPLOAD_ERR_INT_SIZE=>'larger than upload_max_filesize .',
//UPLOAD_ERR_NO_TEMP_DIR=>'no temporary directory .',
$upload_errors=array
(
	UPLOAD_ERR_OK=>'No errors.',
	UPLOAD_ERR_FORM_SIZE=>'Larger than form MAX_FILESIZE .',
	UPLOAD_ERR_PARTIAL=>'Partial upload.',
	UPLOAD_ERR_NO_FILE=>'No file.',
	UPLOAD_ERR_CANT_WRITE=>'Can not write to disk .',
	UPLOAD_ERR_EXTENSION=>'File upload stopped by extension .',
);

if(isset($_POST['submit']))
{
	$filename=$_FILES['uploadimage']['temp_name'];
	$target_file=basename($_FILES['uploadimage']['name']);
	$upload_dir="uploads";
	if(move_uploaded_file($filename,$upload_dir."/".$target_file))
	{
		$msg="File uploaded successifully";
		$server	    = 'localhost';
		$username	= 'root';
		$password	= 'grouptwo';
		$database	= 'test';
		$connect=mysql_connect($server, $username, $password);
		if(!$connect)
		{
	 		exit('Error: can not connect to the server'); 
		}
		$select=mysql_select_db($database);
		if(!$select)
		{
			exit('Error: can not select the database');
		} 	
		$imgData = file_get_contents($filename);
		$size = getimagesize($filename);
		mysql_connect("localhost", "$username", "$password");
		mysql_select_db ("$database");
		$sql = sprintf("INSERT INTO `testlob`(`image_id`, `image_type`, `image`, `image_size`, `image_ctgy`, `image_name`)
    	VALUES('%s', '%s', '%d', '%s')", mysql_real_escape_string($size['mime']), mysql_real_escape_string($imgData),$size[3],
   		mysql_real_escape_string($_FILES['uploadimage']['name']));   
		$insert=mysql_query($sql);
		if(!$insert)
		{
			echo "<p>".mysql_error(). "<p>";
		}	
		
		else
		{
			$error=$_FILES['uploadimage']['error'];
			$msg=$upload_errors[$error];
		}
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>image insertion</title>
</head>
<?PHP if(!empty($msg)){echo '<p>'.$msg.'</p>';}	?>
<body>
<form action="imagepost.php" method="post" enctype="multipart/form-data">
   <input name="MAX_FILE_SIZE" type="hidden" value="2048000	" />
   <input name="uploadimage" type="file" /><input name="upload" type="submit" value="upload" />
</form>

</body>
</html>

